<!doctype html>
<html lang="zh-Hans">
	<head>
		<title>铜线阻值计算器</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<link href="/iframe/css/style.css" rel="stylesheet" />
	</head>
	<body>
		<div class="tablinks active" id="tab-1" onclick="openTab('counter-1')">计算线长</div>
		<div class="tablinks" id="tab-2" onclick="openTab('counter-2')">计算线宽</div>
		<div id="counter-1" class="tabcontent">
			<!-- 阻值 -->
			<label for="resistance-1">阻值（Ω）：</label>
			<input type="number" id="resistance-1" name="resistance-1" value="5" min="0" step="0.01" />

			<!-- 铜片厚度 默认35 -->
			<label for="thickness-1">铜片厚度（μm）：</label>
			<select id="thickness-1" name="thickness-1">
				<option value="12">12</option>
				<option value="18">18</option>
				<option value="35" selected>35</option>
				<option value="70">70</option>
			</select>

			<!-- 线宽 -->
			<label for="lineWidth-1">线宽（mm）：</label>
			<input type="number" id="lineWidth-1" name="lineWidth-1" value="0.254" min="0" step="0.001" />

			<!-- 线长 -->
			<label for="length-1">线长（mm）：</label>
			<input type="number" id="length-1" name="length-1" value="0" disabled />

			<button id="calc-1">计算</button>
		</div>
		<div id="counter-2" class="tabcontent" style="display: none">
			<!-- 阻值 -->
			<label for="resistance-2">阻值（Ω）：</label>
			<input type="number" id="resistance-2" name="resistance-2" value="5" min="0" step="0.01" />

			<!-- 铜片厚度 默认35 -->
			<label for="thickness-2">铜片厚度（μm）：</label>
			<select id="thickness-2" name="thickness-2">
				<option value="12">12</option>
				<option value="18">18</option>
				<option value="35" selected>35</option>
				<option value="70">70</option>
			</select>

			<!-- 线长 -->
			<label for="length-2"
				>线长（mm）：<span id="select-tip-icon"><span>应用于此</span></span></label
			>
			<input type="number" id="length-2" name="length-2" value="100" min="0" step="0.001" />

			<!-- 线宽 -->
			<label for="lineWidth-2">线宽（mm）：</label>
			<input type="number" id="lineWidth-2" name="lineWidth-2" value="0" disabled />

			<button id="calc-2">计算</button>
			<button id="apply-2">应用于<span id="netName">HILBERT</span></button>
		</div>
		<script>
			function openTab(tabName) {
				switch (tabName) {
					case 'counter-1':
						document.getElementById('counter-1').style.display = 'block';
						document.getElementById('counter-2').style.display = 'none';
						document.getElementById('tab-1').classList.add('active');
						document.getElementById('tab-2').classList.remove('active');
						break;
					case 'counter-2':
						document.getElementById('counter-1').style.display = 'none';
						document.getElementById('counter-2').style.display = 'block';
						document.getElementById('tab-1').classList.remove('active');
						document.getElementById('tab-2').classList.add('active');
						break;
				}
			}

			document.getElementById('calc-1').addEventListener('click', function () {
				const resistance = parseFloat(document.getElementById('resistance-1').value);
				const thickness = parseFloat(document.getElementById('thickness-1').value);
				const lineWidth = parseFloat(document.getElementById('lineWidth-1').value);
				const resistivity = 18.51;
				const ratio = (resistance * thickness) / resistivity;
				const length = ratio * lineWidth * 1e3;
				document.getElementById('length-1').value = length.toFixed(3);
			});

			document.getElementById('calc-2').addEventListener('click', function () {
				const resistance = parseFloat(document.getElementById('resistance-2').value);
				const thickness = parseFloat(document.getElementById('thickness-2').value);
				const length = parseFloat(document.getElementById('length-2').value);
				const resistivity = 18.51;
				const lineWidth = (resistivity * length) / (resistance * thickness) / 1e3;
				document.getElementById('lineWidth-2').value = lineWidth.toFixed(3);
			});

			document.getElementById('select-tip-icon').addEventListener('click', function () {
				eda.pcb_SelectControl
					.getAllSelectedPrimitives()
					.then((primitives) => {
						if (primitives.length === 0) {
							eda.sys_Message.showToastMessage('请选择一个网络', 'error');
						} else {
							const netName = primitives[0]['net'];
							eda.pcb_Net.getNetLength(netName).then((length) => {
								length = eda.sys_Unit.milToMm(length);
								document.getElementById('length-2').value = length.toFixed(3);
								document.getElementById('netName').innerText = netName;
								eda.sys_Message.showToastMessage('已选择网络的线长：' + length + 'mm', 'success');
							});
						}
					})
					.catch((error) => {
						eda.sys_Message.showToastMessage(error, 'error');
					});
			});

			document.getElementById('apply-2').addEventListener('click', function () {
				const netName = document.getElementById('netName').innerText;
				const lineWidth = parseFloat(document.getElementById('lineWidth-2').value);
				eda.pcb_Net.getAllPrimitivesByNet(netName).then((primitives) => {
					primitives.forEach((primitive) => {
						console.log(primitive['globalIndex'], { 'lineWidth': eda.sys_Unit.mmToMil(lineWidth) });
						eda.pcb_PrimitiveLine.modify(primitive['globalIndex'], { 'lineWidth': eda.sys_Unit.mmToMil(lineWidth) });
					});
					eda.sys_Message.showToastMessage('已应用线宽：' + lineWidth + 'mm', 'success');
				});
			});
		</script>
	</body>
</html>
